<?php

/* Database processing functions */
/* add functionality with 
require_once('includes/tb_trser_market_database.php'); 
*/
/* test array for the plugin tests */
function tb_trser_market_test_database(){
/* clean up the database */
global $wpdb;
$sql =	'DROP TABLE '.$wpdb->get_blog_prefix().'tb_trser_customer_details;';
$wpdb->query( $sql );
$sql =	'DROP TABLE '.$wpdb->get_blog_prefix().'tb_trser_customer_orders;';
$wpdb->query( $sql );
$sql =	'DROP TABLE '.$wpdb->get_blog_prefix().'tb_trser_invoices;';
$wpdb->query( $sql );
$sql =	'DROP TABLE '.$wpdb->get_blog_prefix().'tb_trser_invoice_details;';
$wpdb->query( $sql );
/* create dummy entrances inside the database for test */
$sql =	"INSERT INTO ".$wpdb->get_blog_prefix()."tb_trser_customer_details (user_id, shipping_address, company_name, customer_name, address, country, identification_code) 
                                                VALUES (1, 'Acasa', 'Linguistic guide', 'Constantin Bogza', 'Str. Radu Voda', 'Romania', '');";
$wpdb->query( $sql );
/* insert one finished item */
$sql =	"INSERT INTO ".$wpdb->get_blog_prefix()."tb_trser_customer_orders (user_id, document_link, document_path, document_name, translation_link, translation_path, status, price, estimated_due_date, upload_date, accepted_date, finished_date, importance) 
                           VALUES (1, 'http://linguisticguide.com/wp-content/uploads/2012/11/50b28391c1501_50a8bf9e60020_Diablo-3-Damage-and-defence-v1.3.2.xlsx', '/home/uidl9703/public_html/wp-content/uploads/2012/11/50b28391c1501_50a8bf9e60020_Diablo-3-Damage-and-defence-v1.3.2.xlsx', '50a8bf9e60020_Diablo-3-Damage-and-defence-v1.3.2.xlsx', 'http://linguisticguide.com/wp-content/uploads/2012/11/50b2877994581_50a8bf9e60020_Diablo-3-Damage-and-defence-v1.3.2.xlsx', '/home/uidl9703/public_html/wp-content/uploads/2012/11/50b2877994581_50a8bf9e60020_Diablo-3-Damage-and-defence-v1.3.2.xlsx', 'finished', '43', '2012-11-30', '2012-11-25', '2012-11-25 21:02:40', '2012-11-25 21:02:49', 'very important');";
$wpdb->query( $sql );
/* insert one canceled item */
$sql =	"INSERT INTO uidl9703_LinGde.LinGdetb_trser_customer_orders (user_id, document_link, document_path, document_name, translation_link, translation_path, status, price, estimated_due_date, upload_date, accepted_date, finished_date, importance) 
                           VALUES (1, 'http://linguisticguide.com/wp-content/uploads/2012/12/50c4e75d35ad7_Platform-development-.doc', '/home/uidl9703/public_html/wp-content/uploads/2012/12/50c4e75d35ad7_Platform-development-.doc', 'Platform development .doc', NULL, NULL, 'canceled', '26', '6', '2012-12-09', '2012-12-09 19:39:00', '', 'not important');";
$wpdb->query( $sql );
/* insert one accepted item */
$sql =	"INSERT INTO uidl9703_LinGde.LinGdetb_trser_customer_orders (user_id, document_link, document_path, document_name, translation_link, translation_path, status, price, estimated_due_date, upload_date, accepted_date, finished_date, importance) 
                            VALUES (1, 'http://linguisticguide.com/wp-content/uploads/2012/12/50c4e95ad032d_Technical-methodology-final.doc', '/home/uidl9703/public_html/wp-content/uploads/2012/12/50c4e95ad032d_Technical-methodology-final.doc', 'Technical methodology final.doc', NULL, NULL, 'accepted', '12', '12', '2012-12-09', '2012-12-09 19:42:53', '', 'less important');";
$wpdb->query( $sql );
/* insert one new item */
$sql =	"INSERT INTO uidl9703_LinGde.LinGdetb_trser_customer_orders (user_id, document_link, document_path, document_name, translation_link, translation_path, status, price, estimated_due_date, upload_date, accepted_date, finished_date, importance) 
                            VALUES (1, 'http://linguisticguide.com/wp-content/uploads/2012/12/50c4e95ad032d_Technical-methodology-final.doc', '/home/uidl9703/public_html/wp-content/uploads/2012/12/50c4e95ad032d_Technical-methodology-final.doc', 'Technical methodology final.doc', NULL, NULL, 'new', '', '', '2012-12-09', '', '', 'less important');";
$wpdb->query( $sql );
/* insert one quoted item */
$sql =	"INSERT INTO uidl9703_LinGde.LinGdetb_trser_customer_orders (user_id, document_link, document_path, document_name, translation_link, translation_path, status, price, estimated_due_date, upload_date, accepted_date, finished_date, importance) 
                            VALUES (1, 'http://linguisticguide.com/wp-content/uploads/2012/12/50c4e95ad032d_Technical-methodology-final.doc', '/home/uidl9703/public_html/wp-content/uploads/2012/12/50c4e95ad032d_Technical-methodology-final.doc', 'Technical methodology final.doc', NULL, NULL, 'quoted', '14', '12', '2012-12-09', '', '', 'less important');";
$wpdb->query( $sql );
}


/* Create the databases at install time */
function tb_trser_market_create_database(){
	global $wpdb;
	/* create the table containing the information about the invoice details */
	$creation_query =
	'CREATE TABLE IF NOT EXISTS '.$wpdb->get_blog_prefix().'tb_trser_customer_details (
                                                                                            `customer_id` int(20) NOT NULL AUTO_INCREMENT,
                                                                                            `user_id` int(20),
                                                                                            `shipping_address` varchar(500),
                                                                                            `company_name` varchar(500),
                                                                                            `customer_name` varchar(500),
                                                                                            `address` varchar(500),
                                                                                            `country` varchar(500),
                                                                                            `identification_code` varchar(500),
                                                                                            PRIMARY KEY (`customer_id`)
                                                                                            );';
	$wpdb->query( $creation_query );
	/* create the table regading the orders that have been placed for translation */
	$creation_query =
	'CREATE TABLE IF NOT EXISTS '.$wpdb->get_blog_prefix().'tb_trser_customer_orders(
                                                                                            `order_id` int(20) NOT NULL AUTO_INCREMENT,
                                                                                            `user_id` int(20),
                                                                                            `document_link` varchar(500),
                                                                                            `document_path` varchar(500),
                                                                                            `document_name` varchar(500),
                                                                                            `importance` varchar(500),
                                                                                            `translation_link` varchar(500),
                                                                                            `translation_path` varchar(500),
                                                                                            `status` varchar(500),
                                                                                            `price` varchar(500),
                                                                                            `estimated_due_date` varchar(500),
                                                                                            `upload_date` date,
                                                                                            `accepted_date` varchar(500),
                                                                                            `finished_date` varchar(500),
                                                                                            PRIMARY KEY (`order_id`)
                                                                                            );';
	$wpdb->query( $creation_query );
	/* create the database for the invoice creation and storing of the data */
	$creation_query =
	'CREATE TABLE IF NOT EXISTS '.$wpdb->get_blog_prefix().'tb_trser_invoices(
                                                                                    `invoice_id` int(20) NOT NULL AUTO_INCREMENT,
                                                                                    `user_id` int(20),
                                                                                    `order_id` int(20),
                                                                                    `invoice_series` varchar(500),
                                                                                    `invoice_no` varchar(500),
                                                                                    `invoice_date` varchar(500),
                                                                                    `customer_name` varchar(500),
                                                                                    `customer_tax_id_code` varchar(500),
                                                                                    `customer_address` varchar(500),
                                                                                    `customer_bank_account` varchar(500),
                                                                                    `customer_bank` varchar(500),
                                                                                    `customer_contact` varchar(500),
                                                                                    `customer_phone` varchar(500),
                                                                                    `customer_email` varchar(500),															
                                                                                    `service_provider_name` varchar(500),
                                                                                    `service_provider_tax_id_code` varchar(500),
                                                                                    `service_provider_licence` varchar(500),
                                                                                    `service_provider_address` varchar(500),
                                                                                    `service_provider_bank_account` varchar(500),
                                                                                    `service_provider_bank` varchar(500),
                                                                                    `service_provider_bank` varchar(500),
                                                                                    `invoice_exchange_rate` float,
                                                                                    PRIMARY KEY (`invoice_id`)
                                                                                    );';
	$wpdb->query( $creation_query );
	
	$creation_query =
	'CREATE TABLE IF NOT EXISTS '.$wpdb->get_blog_prefix().'tb_trser_invoice_details(
                                                                                            `invoice_detail_id` int(20) NOT NULL AUTO_INCREMENT,
                                                                                            `invoice_detail` varchar(500),
                                                                                            `invoice_detail_amount` float,
                                                                                            PRIMARY KEY (`invoice_detail_id`)
                                                                                            );';
	$wpdb->query( $creation_query );
}

/* Customer details database */
function tb_trser_market_get_user_options()
{
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_details` WHERE `user_id` = ".$current_user->ID." LIMIT 0, 30 ";
	$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
	if ( $user_infos ) {
		foreach ( $user_infos as $user_info ) {
			/* user exists inside the database retrive the information */
			$array = array(
			"tb_trser_market_shipping_address" => $user_info['shipping_address'],
			"tb_trser_market_company_name" => $user_info['company_name'],
			"tb_trser_market_name" => $user_info['customer_name'],
			"tb_trser_market_address_name" => $user_info['address'],
			"tb_trser_market_country" => $user_info['country'],
			"tb_trser_market_tax_identification_code" => $user_info['identification_code'] );
		}
	}
	else{
			/* user is non existing in the database replace information */
			$array = array(
			"tb_trser_market_shipping_address" => "",
			"tb_trser_market_company_name" => "",
			"tb_trser_market_name" => $current_user->user_firstname." ".$current_user->user_lastname,
			"tb_trser_market_address_name" => "",
			"tb_trser_market_country" => "",
			"tb_trser_market_tax_identification_code" => "" );
	}
	return $array;
}

function tb_trser_market_set_user_options($user_options)
{
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_details` WHERE `user_id` = ".$current_user->ID." LIMIT 0, 30 ";
	$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
	if ( $user_infos ) {
		/* user is already inside the database update the information */
		$sql = "UPDATE `".$wpdb->base_prefix."tb_trser_customer_details` SET `shipping_address` = '".$user_options['tb_trser_market_shipping_address']."',
															  `company_name` = '".$user_options['tb_trser_market_company_name']."',
															  `customer_name` = '".$user_options['tb_trser_market_name']."',
															  `address` = '".$user_options['tb_trser_market_address_name']."',
															  `country` = '".$user_options['tb_trser_market_country']."',
															  `identification_code` = '".$user_options['tb_trser_market_tax_identification_code']."'
				WHERE `".$wpdb->base_prefix."tb_trser_customer_details`.`user_id` = ".$current_user->ID.";";
	$wpdb->query( $sql );
	}
	else{
		$sql = "INSERT INTO `".$wpdb->base_prefix."tb_trser_customer_details` (`customer_id`, `user_id`, `shipping_address`, `company_name`, `customer_name`, `address`, `country`, `identification_code`) VALUES (NULL,".$current_user->ID.",'".$user_options['tb_trser_market_shipping_address']."','".$user_options['tb_trser_market_company_name']."','".$user_options['tb_trser_market_name']."','".$user_options['tb_trser_market_address_name']."','".$user_options['tb_trser_market_country']."','".$user_options['tb_trser_market_tax_identification_code']."');";
	$wpdb->query( $sql );		
	}
}

/* Customer orders database */

function tb_trser_market_get_user_orders()
{
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_orders` WHERE `user_id` = ".$current_user->ID;
	$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
	if ( $user_infos ) {
		$iCnt = 0;
		foreach ( $user_infos as $user_info ) {
			// user exists inside the database retrive the information 
			$array[$iCnt] = array(
			"tb_trser_market_order_id" => $user_info['order_id'],
			"tb_trser_market_document_link" => $user_info['document_link'],
			"tb_trser_market_translation_link" => $user_info['translation_link'],
			"tb_trser_market_status" => $user_info['status'],
			"tb_trser_market_price" => $user_info['price'],
			"tb_trser_market_estimated_due_date" => $user_info['estimated_due_date'],
			"tb_trser_market_upload_date" => $user_info['upload_date'],
			"tb_trser_market_accepted_date" => $user_info['accepted_date'],
			"tb_trser_market_finished_date" => $user_info['finished_date'],
			"tb_trser_market_document_name" => $user_info['document_name'],
			"tb_trser_market_importance" => $user_info['importance'] );
			$iCnt++;
		}
	}
	return $array;
}

function tb_trser_market_get_new_orders()
{
	global $current_user;
	global $wpdb;
	if (current_user_can('manage_options'))
	{
		get_currentuserinfo();
		$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_orders` WHERE `status` = \"new\"";
		$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
		if ( $user_infos ) {
			$iCnt = 0;
			foreach ( $user_infos as $user_info ) {
				// user exists inside the database retrive the information 
				$array[$iCnt] = array(
				"tb_trser_market_order_id" => $user_info['order_id'],
				"tb_trser_market_document_link" => $user_info['document_link'],
				"tb_trser_market_translation_link" => $user_info['translation_link'],
				"tb_trser_market_status" => $user_info['status'],
				"tb_trser_market_price" => $user_info['price'],
				"tb_trser_market_estimated_due_date" => $user_info['estimated_due_date'],
				"tb_trser_market_upload_date" => $user_info['upload_date'],
				"tb_trser_market_accepted_date" => $user_info['accepted_date'],
				"tb_trser_market_finished_date" => $user_info['finished_date'],
				"tb_trser_market_document_name" => $user_info['document_name'],
				"tb_trser_market_importance" => $user_info['importance'] );
				$iCnt++;
			}
		}
	}
	else
	{
		$array[0] = array("tb_trser_market_document_name" => "You have to be an administrator to view this page" );
	}
	return $array;
}

function tb_trser_market_get_all_orders()
{
	global $current_user;
	global $wpdb;
	if (current_user_can('manage_options'))
	{
		get_currentuserinfo();
		$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_orders`";
		$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
		if ( $user_infos ) {
			$iCnt = 0;
			foreach ( $user_infos as $user_info ) {
				// user exists inside the database retrive the information 
				$array[$iCnt] = array(
				"tb_trser_market_order_id" => $user_info['order_id'],
				"tb_trser_market_document_link" => $user_info['document_link'],
				"tb_trser_market_translation_link" => $user_info['translation_link'],
				"tb_trser_market_status" => $user_info['status'],
				"tb_trser_market_price" => $user_info['price'],
				"tb_trser_market_estimated_due_date" => $user_info['estimated_due_date'],
				"tb_trser_market_upload_date" => $user_info['upload_date'],
				"tb_trser_market_accepted_date" => $user_info['accepted_date'],
				"tb_trser_market_finished_date" => $user_info['finished_date'],
				"tb_trser_market_document_name" => $user_info['document_name'],
				"tb_trser_market_importance" => $user_info['importance'] );
				$iCnt++;
			}
		}
	}
	else
	{
		$array[0] = array("tb_trser_market_document_name" => "You have to be an administrator to view this page" );
	}
	return $array;
}

/* gets one order from the table */
function tb_trser_market_get_order_id($order_id)
{
	global $current_user;
	global $wpdb;
	if (is_user_logged_in())
	{
		get_currentuserinfo();
		$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_orders` WHERE `order_id` = ".$order_id;
		$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
		if ( $user_infos ) {
			foreach ( $user_infos as $user_info ) {
				// user exists inside the database retrive the information 
				$array = array(
				"tb_trser_market_order_id" => $user_info['order_id'],
				"tb_trser_market_document_link" => $user_info['document_link'],
				"tb_trser_market_translation_link" => $user_info['translation_link'],
				"tb_trser_market_status" => $user_info['status'],
				"tb_trser_market_price" => $user_info['price'],
				"tb_trser_market_estimated_due_date" => $user_info['estimated_due_date'],
				"tb_trser_market_upload_date" => $user_info['upload_date'],
				"tb_trser_market_accepted_date" => $user_info['accepted_date'],
				"tb_trser_market_finished_date" => $user_info['finished_date'],
				"tb_trser_market_document_name" => $user_info['document_name'],
				"tb_trser_market_importance" => $user_info['importance'] );
			}
		}
	}
	else
	{
		$array = array("tb_trser_market_document_name" => "You have to be an administrator to view this page" );
	}
	return $array;
}

/* gets email from order id */
function tb_trser_market_get_user_email($order_id)
{
	global $current_user;
	global $wpdb;
	if (is_user_logged_in())
	{
		get_currentuserinfo();
		$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_orders` WHERE `order_id` = ".$order_id;
		$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
		if ( $user_infos ) {
			foreach ( $user_infos as $user_info ) {
				// user exists inside the database retrive the information 
				$user_id = $user_info['user_id'];
				$user_info = get_userdata($user_id);
				$return = $user_info->user_email;
			}
		}
	}
	else
	{
		$return = "contact@linguisticguide.com";
	}
	return $return;
}

/* function used to quote a user order */
function tb_trser_market_quote_user_order($user_order){
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	if(!(current_user_can('manage_options')||tb_trser_market_check_is_user_order($user_order))){
		wp_die("You are not allowed to perform update operations! Error code : ".current_user_can('manage_options')." ".tb_trser_market_check_is_user_order($user_order));
	}
	else{
		$sql = "UPDATE `".$wpdb->base_prefix."tb_trser_customer_orders` SET `status` = \"quoted\", `price` =".$user_order['tb_trser_market_price'].", `estimated_due_date` = \"".$user_order['tb_trser_market_due_date']."\" WHERE `order_id` = ".$user_order['tb_trser_market_order_id'].";";
		$wpdb->query( $sql ); 
	}
}

/* function used to accept a user order */
function tb_trser_market_accept_user_order($user_order){
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	if(!(current_user_can('manage_options')||tb_trser_market_check_is_user_order($user_order))){
		wp_die("You are not allowed to perform update operations!");
	}
	else{
		$sql = "UPDATE `".$wpdb->base_prefix."tb_trser_customer_orders` SET `status` = \"accepted\", `accepted_date` = \"".date('Y-m-d H:i:s')."\" WHERE `order_id` = ".$user_order['tb_trser_market_order_id'].";";
		$wpdb->query( $sql ); 
	}
}

/* function used to cancel a user order */
function tb_trser_market_cancel_user_order($user_order){
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	if(!(current_user_can('manage_options')||tb_trser_market_check_is_user_order($user_order))){
		wp_die("You are not allowed to perform update operations!");
	}
	else{
		$sql = "UPDATE `".$wpdb->base_prefix."tb_trser_customer_orders` SET `status` = \"canceled\", `accepted_date` = \"".date('Y-m-d H:i:s')."\" WHERE `order_id` = ".$user_order['tb_trser_market_order_id'].";";
		$wpdb->query( $sql ); 
	}
}

/* function used to accept a user order */
function tb_trser_market_start_user_order($user_order){
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	if(!(current_user_can('manage_options')||tb_trser_market_check_is_user_order($user_order))){
		wp_die("You are not allowed to perform update operations!");
	}
	else{
		$sql = "UPDATE `".$wpdb->base_prefix."tb_trser_customer_orders` SET `status` = \"in translation\" WHERE `order_id` = ".$user_order['tb_trser_market_order_id'].";";
		$wpdb->query( $sql ); 
	}
}

/* function used to accept a user order */
function tb_trser_market_finish_user_order($user_order){
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	if(!current_user_can('manage_options')){
		wp_die("You are not allowed to perform update operations!");
	}
	else{
		$sql = "UPDATE `".$wpdb->base_prefix."tb_trser_customer_orders` SET `status` = \"finished\" WHERE `order_id` = ".$user_order['tb_trser_market_order_id'].";";
		$status .= $wpdb->query( $sql ); 
	}
	return $status;
}

/* checks if user is allowed to make operations inside the database */
function tb_trser_market_check_is_user_order($user_order) {
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_orders` WHERE `user_id` = ".$current_user->ID." AND `order_id` =".$user_order['tb_trser_market_order_id'].";";
	$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
	if ( $user_infos ) return 1;
	else return 0;
} 

/* updates the table with the translation */
function tb_trser_market_set_user_translation($user_order)
{
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	if(isset($user_order['tb_trser_market_order_id'])){
	// one order has to be modified 
	$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_orders` WHERE `order_id` =".$user_order['tb_trser_market_order_id'];
	$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
		if ( $user_infos ) {
			// the entry exists have to update the content 
			$sql = "UPDATE `".$wpdb->base_prefix."tb_trser_customer_orders` SET ";
			$sql .= "`translation_path` = \"".$user_order['tb_trser_market_document_path']."\",";
			$sql .= "`translation_link` = \"".$user_order['tb_trser_market_document_link']."\",";
			$sql .= "`finished_date` = \"".date('Y-m-d H:i:s')."\",";
			$sql .= "`status` = \"finished\" ";
			$sql .= " WHERE `order_id` = ".$user_order['tb_trser_market_order_id'].";";
			$status = $wpdb->query( $sql ); 
		}
	}
	return $status;
}

function tb_trser_market_set_user_order($user_order)
{
	global $current_user;
	global $wpdb;
    get_currentuserinfo();
	if(isset($user_order['tb_trser_market_order_id'])){
	// one order has to be modified 
	$sql = "SELECT * FROM `".$wpdb->base_prefix."tb_trser_customer_orders` WHERE `user_id` = ".$current_user->ID." AND `order_id` =".$user_order['tb_trser_market_order_id'];
	$user_infos = $wpdb->get_results( $wpdb->prepare( $sql ), ARRAY_A );
		if ( $user_infos ) {
			// the entry exists have to update the content 
 			$sql = "UPDATE `".$wpdb->base_prefix."tb_trser_customer_details` SET";
			if(isset($user_order['tb_trser_market_document_link'])) $sql .= "`document_link` = '".$user_order['tb_trser_market_document_link'];
			if(isset($user_order['tb_trser_market_status'])||isset($user_order['tb_trser_market_price'])||isset($user_order['tb_trser_market_estimated_due_date'])) $sql .= ", ";
			if(isset($user_order['tb_trser_market_status']))	$sql .= "`status` = '".$user_order['tb_trser_market_status']."'";
			if(isset($user_order['tb_trser_market_price'])||isset($user_order['tb_trser_market_estimated_due_date'])) $sql .= ", ";
			if(isset($user_order['tb_trser_market_price']))	$sql .= "`price` = '".$user_order['tb_trser_market_price']."'";
			if(isset($user_order['tb_trser_market_estimated_due_date'])) $sql .= ", ";
			if(isset($user_order['tb_trser_market_estimated_due_date'])) $sql .= "`estimated_due_date` = '".$user_order['tb_trser_market_estimated_due_date']."'";
			$sql .= "WHERE `wpTest_tb_trser_customer_orders`.`user_id` = ".$current_user->ID." AND `order_id` =".$user_order['tb_trser_market_order_id'].";";
		$wpdb->query( $sql ); 
		}
	}
	else{
	// this is a new upload of an order 
		$sql = "INSERT INTO `".$wpdb->base_prefix."tb_trser_customer_orders` (`order_id`, `user_id`, `document_link`, `document_path`, `document_name`,`importance`,`status`, `price`, `estimated_due_date`, `upload_date`) VALUES (NULL,'".$current_user->ID."','".$user_order['tb_trser_market_document_link']."','".$user_order['tb_trser_market_document_path']."','".$user_order['tb_trser_market_document_name']."','".$user_order['tb_trser_market_importance']."','new','not quoted','not estimated','".date('Y-m-d H:i:s')."');";
	$wpdb->query( $sql );
	}
}

function tb_trser_market_valid_date($data)
{
	if (date('Y-m-d', strtotime($data)) == $data) {
		return true;
	} else {
		return false;
	}
} 

?>